package com.powernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.StoreConstants;
import com.powernode.domain.Notice;
import com.powernode.mapper.NoticeMapper;
import com.powernode.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

@Service
@CacheConfig(cacheNames = "com.powernode.service.impl.NoticeServiceImpl")
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;


    @Override
    @CacheEvict(key = StoreConstants.WX_TOP_NOTICE)
    public Boolean saveNotice(Notice notice) {
        notice.setShopId(1L);
        return noticeMapper.insert(notice) > 0;
    }

    @Override
    @CacheEvict(key = StoreConstants.WX_TOP_NOTICE)
    public Boolean modifyNotice(Notice notice) {
        return noticeMapper.updateById(notice) > 0;
    }

    @Override
    @CacheEvict(key = StoreConstants.WX_TOP_NOTICE)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    /**
     * 查询微信顶部公告列表
     *
     * @return 返回微信顶部公告列表
     * @Cacheable 注解表示该方法的返回值会被缓存起来，下次调用时直接从缓存中获取结果，而不是重新执行方法。
     * key = StoreConstants.WX_TOP_NOTICE 指定了缓存的键名，即缓存的唯一标识。
     */
    @Override
    @Cacheable(key = StoreConstants.WX_TOP_NOTICE)
    public List<Notice> queryWxTopNoticeList() {
        return noticeMapper.selectList(new LambdaQueryWrapper<Notice>().eq(Notice::getStatus, 1).eq(Notice::getIsTop, 1).orderByDesc(Notice::getCreateTime));
    }

    /**
     * 查询微信所有公告列表
     *
     * @return 返回微信所有公告的列表
     *
     * @Cacheable 注解表示该方法的返回值会被缓存起来，下次调用时直接从缓存中获取结果，而不是重新执行方法。
     * key = StoreConstants.WX_ALL_NOTICE 指定了缓存的键名，即缓存的唯一标识。
     */
    @Override
    @Cacheable(key = StoreConstants.WX_ALL_NOTICE)
    public List<Notice> queryWxAllNoticeList() {
        return noticeMapper.selectList(new LambdaQueryWrapper<Notice>().eq(Notice::getStatus, 1).orderByDesc(Notice::getCreateTime));
    }
}
